{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "ndarray数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "创建数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1=np.array([1,2,3,4,5])\n",
    "arr1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr2=np.array([[1,2,3],[4,5,6]])\n",
    "arr2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "ndarray的基础属性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1.ndim #数组维度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr2.ndim"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(5,)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1.shape #数组的形状(行、列)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2, 3)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr2.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr2.size"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1.size  # 输出数组的元素个数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('int64')"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1.dtype # 查看数据类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('int64')"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr2.dtype"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "创建数组的便捷函数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "ones"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1., 1., 1., 1., 1., 1., 1., 1.])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.ones(8)  # 创建一个8位的全1数组\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 1, 1],\n",
       "       [1, 1, 1]], dtype=int32)"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.ones(dtype=np.int32, shape=(2, 3))  # 创建一个全1的ndarray，数据类型为int32，形状为(2, 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 1, 1],\n",
       "       [1, 1, 1]])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.ones_like(arr2) #创建与arr2形状相同的全1数组"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "zeros"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0., 0., 0., 0.],\n",
       "       [0., 0., 0., 0.],\n",
       "       [0., 0., 0., 0.]], dtype=float32)"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.zeros(shape=(3, 4),dtype=np.float32)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "full  创建某个固定数值的数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[666, 666, 666],\n",
       "       [666, 666, 666]])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a=np.full((2, 3), 666)\n",
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "arange  创建等差数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1., 2., 3., 4., 5., 6., 7., 8., 9.], dtype=float32)"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.arange(1,10,dtype=np.float32)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1,  3,  5,  7,  9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33,\n",
       "       35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67,\n",
       "       69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.arange(1,100,2) #从1 到 100 步长为 2 的数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2, 3, 4],\n",
       "       [5, 6, 7, 8, 9]])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.arange(10).reshape(2,5)  # 将数组重构成2行5列的ndarray"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "linspace"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.,  2.,  4.,  6.,  8., 10.])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.linspace(0, 10, 6)  #在0-10 创建等差数组，数量为6"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "random模块的使用 返回0-1之间的随机数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.5744946 , 0.53870745, 0.09172546, 0.81954264, 0.67608982])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.random(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.40840179, 0.17244542],\n",
       "       [0.70831366, 0.64086668],\n",
       "       [0.66231212, 0.81148026]])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.random((3, 2))  #注意传入的是元组，返回3行2列的数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.2078739 , 0.74883065, 0.26746309, 0.50057322, 0.78005916],\n",
       "       [0.45349396, 0.54649386, 0.45318831, 0.2859866 , 0.9060345 ],\n",
       "       [0.47258677, 0.99073927, 0.17141235, 0.04749409, 0.39121412],\n",
       "       [0.41924021, 0.28982579, 0.84484536, 0.89814908, 0.42917182],\n",
       "       [0.35033807, 0.11528936, 0.13939896, 0.15136435, 0.89830538]])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.rand(5,5) #生成5x5的随机数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[0.45997983, 0.25227832, 0.57013501, 0.80032112],\n",
       "        [0.5748954 , 0.20740078, 0.23538253, 0.87014175],\n",
       "        [0.72523784, 0.26483646, 0.98655208, 0.90277446]],\n",
       "\n",
       "       [[0.45304243, 0.35626714, 0.5368034 , 0.00477127],\n",
       "        [0.07156451, 0.63624651, 0.53543514, 0.50805144],\n",
       "        [0.86772714, 0.32600621, 0.5598556 , 0.60356547]]])"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.rand(2,3,4) #三维数组"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "randint 生成整数随机数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.randint(0, 10) # 随机生成一个0-9之间的整数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3, 1, 8],\n",
       "       [0, 5, 7],\n",
       "       [1, 7, 2]], dtype=int32)"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.randint(0, 10, size=(3, 3))  #在0-10之间随机生成3x3的整数数组"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "seed 随机种子，指定随机种子后，结果相同"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.77132064, 0.02075195, 0.63364823, 0.74880388],\n",
       "       [0.49850701, 0.22479665, 0.19806286, 0.76053071],\n",
       "       [0.16911084, 0.08833981, 0.68535982, 0.95339335]])"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.seed(10)\n",
    "np.random.rand(3, 4)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "randn()函数可以生成服从正态分布的随机数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.22863013,  0.44513761, -1.13660221,  0.13513688,  1.484537  ])"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.randn(5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "normal #生成高斯分布的随机数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-1069.80488579, -1967.72828066, -1733.3722959 ,   276.070164  ,\n",
       "        2394.96733071,  1133.69125341,  1682.62221331,   109.14921584,\n",
       "        1407.99637709,  -261.24798797])"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.normal(10,1000,10)  #均值10 标准差1000 产生10个随机数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 623.20418486, -257.31718867, -539.30901411,  142.70829576],\n",
       "       [-466.14201493, 1318.47308086,  205.01327923,  410.20998825],\n",
       "       [-327.63233709, 1266.47226376, -721.969502  ,  670.23155123]])"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.normal(10, 1000, size=(3, 4))   # 产生3行4列的随机数，均值为10，标准差为1000"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "uniform  返回均匀分布的随机数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.3255116378322488"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.uniform() "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.660063590876594"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.uniform(1,5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([4.5327632 , 1.84114337, 8.38995092, 2.36036818, 4.45703004])"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.uniform(1, 10, 5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[9.49834641, 9.88862927, 5.10674092],\n",
       "       [8.43510559, 3.26236721, 6.37634483],\n",
       "       [9.12548584, 5.81102154, 6.31181227]])"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.uniform(1, 10, size=(3, 3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on method uniform in module numpy.random.mtrand:\n",
      "\n",
      "uniform(low=0.0, high=1.0, size=None) method of numpy.random.mtrand.RandomState instance\n",
      "    uniform(low=0.0, high=1.0, size=None)\n",
      "\n",
      "    Draw samples from a uniform distribution.\n",
      "\n",
      "    Samples are uniformly distributed over the half-open interval\n",
      "    ``[low, high)`` (includes low, but excludes high).  In other words,\n",
      "    any value within the given interval is equally likely to be drawn\n",
      "    by `uniform`.\n",
      "\n",
      "    .. note::\n",
      "        New code should use the `~numpy.random.Generator.uniform`\n",
      "        method of a `~numpy.random.Generator` instance instead;\n",
      "        please see the :ref:`random-quick-start`.\n",
      "\n",
      "    Parameters\n",
      "    ----------\n",
      "    low : float or array_like of floats, optional\n",
      "        Lower boundary of the output interval.  All values generated will be\n",
      "        greater than or equal to low.  The default value is 0.\n",
      "    high : float or array_like of floats\n",
      "        Upper boundary of the output interval.  All values generated will be\n",
      "        less than or equal to high.  The high limit may be included in the\n",
      "        returned array of floats due to floating-point rounding in the\n",
      "        equation ``low + (high-low) * random_sample()``.  The default value\n",
      "        is 1.0.\n",
      "    size : int or tuple of ints, optional\n",
      "        Output shape.  If the given shape is, e.g., ``(m, n, k)``, then\n",
      "        ``m * n * k`` samples are drawn.  If size is ``None`` (default),\n",
      "        a single value is returned if ``low`` and ``high`` are both scalars.\n",
      "        Otherwise, ``np.broadcast(low, high).size`` samples are drawn.\n",
      "\n",
      "    Returns\n",
      "    -------\n",
      "    out : ndarray or scalar\n",
      "        Drawn samples from the parameterized uniform distribution.\n",
      "\n",
      "    See Also\n",
      "    --------\n",
      "    randint : Discrete uniform distribution, yielding integers.\n",
      "    random_integers : Discrete uniform distribution over the closed\n",
      "                      interval ``[low, high]``.\n",
      "    random_sample : Floats uniformly distributed over ``[0, 1)``.\n",
      "    random : Alias for `random_sample`.\n",
      "    rand : Convenience function that accepts dimensions as input, e.g.,\n",
      "           ``rand(2,2)`` would generate a 2-by-2 array of floats,\n",
      "           uniformly distributed over ``[0, 1)``.\n",
      "    random.Generator.uniform: which should be used for new code.\n",
      "\n",
      "    Notes\n",
      "    -----\n",
      "    The probability density function of the uniform distribution is\n",
      "\n",
      "    .. math:: p(x) = \\frac{1}{b - a}\n",
      "\n",
      "    anywhere within the interval ``[a, b)``, and zero elsewhere.\n",
      "\n",
      "    When ``high`` == ``low``, values of ``low`` will be returned.\n",
      "    If ``high`` < ``low``, the results are officially undefined\n",
      "    and may eventually raise an error, i.e. do not rely on this\n",
      "    function to behave when passed arguments satisfying that\n",
      "    inequality condition. The ``high`` limit may be included in the\n",
      "    returned array of floats due to floating-point rounding in the\n",
      "    equation ``low + (high-low) * random_sample()``. For example:\n",
      "\n",
      "    >>> x = np.float32(5*0.99999999)\n",
      "    >>> x\n",
      "    5.0\n",
      "\n",
      "\n",
      "    Examples\n",
      "    --------\n",
      "    Draw samples from the distribution:\n",
      "\n",
      "    >>> s = np.random.uniform(-1,0,1000)\n",
      "\n",
      "    All values are within the given interval:\n",
      "\n",
      "    >>> np.all(s >= -1)\n",
      "    True\n",
      "    >>> np.all(s < 0)\n",
      "    True\n",
      "\n",
      "    Display the histogram of the samples, along with the\n",
      "    probability density function:\n",
      "\n",
      "    >>> import matplotlib.pyplot as plt\n",
      "    >>> count, bins, ignored = plt.hist(s, 15, density=True)\n",
      "    >>> plt.plot(bins, np.ones_like(bins), linewidth=2, color='r')\n",
      "    >>> plt.show()\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(np.random.uniform)  #查阅文档"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "machineLearn",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
